using System.Collections.Generic;
using System.Text;
using ViScript.Core.Blocks;

namespace ViScript.Core.Blocks
{
    public abstract class NestedBlock : IBlock
    {
        private readonly NestedBlock parent;
        private readonly List<IBlock> blocks;
        private IBlock currentBlock;

        public NestedBlock(NestedBlock parent)
        {
            this.parent = parent;
            blocks = new List<IBlock>();
        }

        public virtual string Escape()
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (IBlock block in blocks)
            {
                stringBuilder.Append(block.Escape());
            }
            return stringBuilder.ToString();
        }

        public virtual string Unescape()
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (IBlock block in blocks)
            {
                stringBuilder.Append(block.Unescape());
            }
            return stringBuilder.ToString();
        }

        public abstract void Append(char c);
        public abstract bool ShouldAppendQuotedBlock(char c);

        protected NestedBlock Parent
        {
            get { return parent; }
        }

        protected IBlock CurrentBlock
        {
            get { return currentBlock; }
        }

        public void AppendBlock(IBlock block)
        {
            blocks.Add(block);
            currentBlock = block;
        }
    }
}